import React from "react";
import routes from "./routes";
import {
  createBrowserRouter,
  Navigate,
  RouterProvider,
  useRouteError,
} from "react-router-dom";
import type { RouteObject } from "react-router";
import Layout from "./Layout";
const Error = () => {
  const error = useRouteError();
  if (error.status === 404) {
    return <Navigate to="/form/select" replace />;
  }
  return <div>错误</div>;
};
const rootRoute = [
  {
    path: "/",
    element: <Layout />,
    errorElement: <Error />,
    children: routes,
  },
];
const router = () => {
  return createBrowserRouter(rootRoute as RouteObject[]);
};
export default () => {
  return (
    <RouterProvider router={router()} fallbackElement={<p>Loading...</p>} />
  );
};
